import java.util.LinkedList;

/* 
 * A parser for the SDP protocol specified in the RFC 2327 (http://www.ietf.org/rfc/rfc2327.txt)
 */
public class SDPparser {

	String v, o, s, i, u, e, p, c, b, z, k, a;
	LinkedList<String> sAttri = new LinkedList<String>();
	LinkedList<String> tAttri = new LinkedList<String>();
	LinkedList<String> mAttri = new LinkedList<String>();

	public SDPparser() {
 
	}


	/*
	 * Parse data and put it into this parsers map
	 * 
	 * v=  (protocol version)
        o=  (owner/creator and session identifier).
        s=  (session name)
        i=* (session information)
        u=* (URI of description)
        e=* (email address)
        p=* (phone number)
        c=* (connection information - not required if included in all media)
        b=* (bandwidth information)
        One or more time descriptions (see below)
        z=* (time zone adjustments)
        k=* (encryption key)
        a=* (zero or more session attribute lines)
        
	 */
	public String parse(String sdpp) {
		
		String[] exploded = sdpp.split("\r\n");
		v = o = s = i = u = e = p = c = b = z = k = a = "";
		sdpp = "";
		boolean gotM = false;
		for(int it = 0; it < exploded.length; ++it){
			String[] reqAttr = exploded[it].split("=");
			if(reqAttr[0].equals("v")){
				v = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("o")){
				o = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("s")){
				s = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("s")){
				s = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("i")){
				i = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("u")){
				u = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("e")){
				e = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("p")){
				p = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("c")){
				c = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("b")){
				b = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("z")){
				z = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("k")){
				k = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("t")){
				tAttri.add(exploded[it] + "\r\n");
			}else if(reqAttr[0].equals("c")){
				c = exploded[it] + "\r\n";
			}else if(reqAttr[0].equals("m")){
				mAttri.add(exploded[it] + "\r\n");
				gotM = true;
			}else if(reqAttr[0].equals("a") && !gotM){
				sAttri.add(exploded[it] + "\r\n");
			}else if(reqAttr[0].equals("a") && gotM){
				mAttri.add(exploded[it] + "\r\n");
			}
		}
		if(v.equals("") || o.equals("") || s.equals("") || tAttri.isEmpty() || mAttri.isEmpty()){
			debug("Bad request from caller");
			return "ERROR";
		}
		sdpp +=  v;
		sdpp +=  o;
		sdpp +=  s;
		sdpp +=  c;
		for(int it = 0; it < tAttri.size(); ++it){
			sdpp +=  tAttri.get(it);			
		}
		for(int it = 0; it < sAttri.size(); ++it){
			sdpp +=  sAttri.get(it);			
		}
		for(int it = 0; it < mAttri.size(); ++it){
			sdpp +=  mAttri.get(it);			
		}

		return sdpp; //Will be null if everything went OK
	}


	/*
	 * Print debugging information
	 */
	private void debug(Object msg) {
		System.err.println("== DEBUG in SDPparser.java: " + msg.toString());
	}

	private void error(Object msg) {
		System.err.println("== ERROR in SDPparser.java: " + msg.toString());
	}

}
